package com.hao.epidemic_prevention.security.handler;

import com.hao.epidemic_prevention.utils.JSONResult;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
    private String url;
    private boolean isRedirect;
    public MyAuthenticationFailureHandler(String url, boolean isRedirect){
        this.url = url;
        this.isRedirect = isRedirect;
    }

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        System.out.println("自定义登录失败处理器执行。。。。。。。。。");

        httpServletResponse.setContentType("application/json;charset=utf-8");
        JSONResult result = new JSONResult(500, "用户名或密码错误！", null);
        httpServletResponse.getWriter().write(result.toString());
        /*if(isRedirect){
            httpServletResponse.sendRedirect(url);
        }else{
            httpServletRequest.getRequestDispatcher(url).forward(httpServletRequest,httpServletResponse);
        }*/
    }
}
